package com.uit.se6.customui.democustomui.sidedrawer;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.uit.se6.customui.android.navigation.SideDrawer.SideDrawerActivity2;
import com.uit.se6.customui.android.navigation.SideDrawer.SideDrawerItem;
import com.uit.se6.customui.democustomui.R;

public class MainActivity2 extends SideDrawerActivity2 {

	// slide menu items
	private String[] navMenuTitles;
	private TypedArray navMenuIcons;
	private ArrayList<SideDrawerItem> sideDrawerItems;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sidedrawer_activity_main);

		ListView listView = (ListView) this.findViewById(R.id.list_slidermenu);
		DrawerLayout drawerLayout = (DrawerLayout) this
				.findViewById(R.id.drawer_layout);

		this.initializeLayout(drawerLayout, listView, R.id.frame_container);

		this.setupContainer(new SideDrawerListener() {
			@Override
			public Fragment getSelectedFragment(int position) {
				return getView(position);
			}

			@Override
			public Activity getActivity() {
				return MainActivity2.this;
			}
		});

		navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
		navMenuIcons = getResources()
				.obtainTypedArray(R.array.nav_drawer_icons);

		sideDrawerItems = new ArrayList<SideDrawerItem>();
		sideDrawerItems.add(new SideDrawerItem(navMenuTitles[0], navMenuIcons
				.getResourceId(0, -1)));
		sideDrawerItems.add(new SideDrawerItem(navMenuTitles[1], navMenuIcons
				.getResourceId(1, -1)));
		sideDrawerItems.add(new SideDrawerItem(navMenuTitles[2], navMenuIcons
				.getResourceId(2, -1)));
		// add a counter here
		sideDrawerItems.add(new SideDrawerItem(navMenuTitles[3], navMenuIcons
				.getResourceId(3, -1), true, "22"));
		sideDrawerItems.add(new SideDrawerItem(navMenuTitles[4], navMenuIcons
				.getResourceId(4, -1)));
		sideDrawerItems.add(new SideDrawerItem(navMenuTitles[5], navMenuIcons
				.getResourceId(5, -1), true, "50+"));
		// Recycle the typed array
		navMenuIcons.recycle();

		this.initializeMenu(R.drawable.ic_drawer, R.string.app_name,
				sideDrawerItems);

		if (savedInstanceState == null) {
			this.displayView(0);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	private Fragment getView(int position) {
		// update the main content by replacing fragments
		Fragment fragment = null;
		switch (position) {
		case 0:
			fragment = new HomeFragment();
			break;
		case 1:
			sideDrawerItems.get(3).setCount("80");
			this.notifySideDrawerItem(sideDrawerItems);
			fragment = new FindPeopleFragment();
			break;
		case 2:
			fragment = new PhotosFragment();
			break;
		case 3:
			fragment = new CommunityFragment();
			break;
		case 4:
			fragment = new PagesFragment();
			break;
		case 5:
			fragment = new WhatsHotFragment();
			break;

		default:
			break;
		}
		return fragment;
	}
}
